<!--
 * @Date: 2022-04-26 11:27:33
 * @LastEditors: wuyuxin
 * @LastEditTime: 2022-04-28 14:43:26
 * @FilePath: \Vue3-ts-cms\src\components\page-content\src\page-content.vue
 * @description: 文件描述
-->
<template>
  <div class="page-content">
    <my-table
      v-bind="contentTableConfig"
      :listData="listData"
      :listCount="listCount"
      v-model:page="pageInfo"
    >
      <!-- 1.header中的插槽 -->
      <template #headerHandler>
        <el-button type="primary" v-if="isCreate" @click="handleCreateClick"
          >新建</el-button
        >
      </template>
      <template #status="scope">
        <el-button
          size="small"
          :type="scope.row.enable ? 'success' : 'danger'"
          >{{ scope.row.enable ? '启用' : '禁用' }}</el-button
        >
      </template>
      <template #createAt="scope">
        <span>{{ $filters.formatUtcTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatUtcTime(scope.row.updateAt) }}</span>
      </template>
      <template #handler="scope">
        <div class="handle-btns">
          <el-button
            size="small"
            type="text"
            v-if="isUpdate"
            @click="handleUpdateClick(scope.row)"
            ><el-icon><edit /></el-icon>编辑</el-button
          >
          <el-button
            size="small"
            type="text"
            v-if="isDelete"
            @click="handleDeleteClick(scope.row)"
            ><el-icon><delete /></el-icon>删除</el-button
          >
        </div>
      </template>
      <template
        v-for="item in otherSlotOptions"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </my-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useStore } from 'vuex'
import { userPermissionHook } from '@/hooks/userPermissionHook'
import MyTable from '@/base-ui/table'
export default defineComponent({
  components: {
    MyTable
  },
  props: {
    contentTableConfig: {
      type: Object,
      require: true
    },
    moduleName: {
      type: String,
      require: true
    },
    pageName: {
      type: String,
      require: true
    },
    getListUrl: {
      type: String,
      require: true
    }
  },
  emits: ['handleUpdateClick', 'handleCreateClick'],
  setup(props, { emit }) {
    // 0.获取操作的权限
    const isCreate = userPermissionHook(props.pageName, 'create')
    const isUpdate = userPermissionHook(props.pageName, 'update')
    const isDelete = userPermissionHook(props.pageName, 'delete')
    const isQuery = userPermissionHook(props.pageName, 'query')
    console.log(isDelete, 'isDelete')
    const store = useStore()
    // 1.双向绑定pageInfo
    const pageInfo = ref({
      currentPage: 1,
      pageSize: 10
    })
    watch(pageInfo, () => getDataList())

    // 2.发送网络请求
    const getDataList = (payload?: any) => {
      store.dispatch(`${props.moduleName}/getPageListAction`, {
        pageUrl: props.getListUrl,
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...payload
        }
      })
    }
    getDataList()

    // 3.从vuex中获取数据
    const listData = computed(() =>
      store.getters[`${props.moduleName}/pageListData`](props.pageName)
    )
    const listCount = computed(() =>
      store.getters[`${props.moduleName}/pageListCount`](props.pageName)
    )

    // 4.获取其他的动态插槽名称
    const otherSlotOptions = props.contentTableConfig?.propList.filter(
      (item: any) => {
        if (item.slotName === 'status') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )

    // 5.删除/编辑/新建操作
    const handleDeleteClick = (item: any) => {
      console.log(item)
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }
    const handleUpdateClick = (item: any) => {
      emit('handleUpdateClick', item)
    }
    const handleCreateClick = () => {
      emit('handleCreateClick')
    }

    return {
      listData,
      listCount,
      getDataList,
      pageInfo,
      otherSlotOptions,
      isCreate,
      isUpdate,
      isDelete,
      isQuery,
      handleDeleteClick,
      handleUpdateClick,
      handleCreateClick
    }
  }
})
</script>

<style lang="less" scoped>
.page-content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
